package com.jnshu.web;

import com.jnshu.dto.person.UserDto;
import com.jnshu.exception.MyException;
import com.jnshu.pojo.User;
import com.jnshu.service.PersonService;
import com.jnshu.tools.JwtUtil;
import com.jnshu.tools.binding.JsonUtil;
import com.jnshu.tools.resultUtils.PageBean;
import com.jnshu.tools.resultUtils.RUtil;
import com.jnshu.tools.resultUtils.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
public class PersonController {
    //编译器报错，无视。 因为这个Bean是在程序启动的时候注入的，编译器感知不到，所以报错,加上required=false可以屏蔽掉。
    @Autowired(required = false)
    PersonService personService;

    //前台微信登录
    @GetMapping(value = "/u/login/code")
    ResultBean userLogin(@RequestParam("code") String code, HttpServletResponse response) {
        log.info("feign-web获取到的code码是：" + code);
        UserDto userDto = personService.userLogin(code);
        log.info("feign-web由code获取到的UserDto为：" + userDto);
        String token;
        try {
            token = JwtUtil.createToken(userDto.getId());
            log.info("feign-web添加到token中的数据id为：" + userDto.getId());
        } catch (Exception e) {
            throw new MyException(10000, "token加密失败");
        }
        response.setHeader("token", token);
        userDto.setToken(token);
        log.info("feign-web中生成的token添加到header请求头中" + token);
        return RUtil.ok(userDto);
    }

    //前台发送手机验证码
    @PostMapping(value = "/u/phone/code")
    ResultBean sendPhoneCode(@RequestParam("phone") String phone) {
        log.info("浏览器发送的手机号码是：" + phone);
        return personService.sendPhoneCode(phone);
    }

    //绑定手机号码
    @PostMapping(value = "/u/phone/binding")
    ResultBean phoneBinding(@RequestParam("phone") String phone,
                            @RequestParam("code") String code,
                            HttpServletRequest request) {
        long id = Long.parseLong((String) request.getAttribute("id"));
        log.info("浏览器发送的手机号码phone是：" + phone + ",验证码code是：" + code + ",用户id为：" + id);
        return personService.phoneBinding(phone, code, id);
    }

    //前台发送邮箱验证码
    @PostMapping(value = "/u/my/email/code")
    ResultBean sendEmailCode(@RequestParam("email") String email) {
        log.info("浏览器发送的邮箱地址是：" + email);
        return personService.sendEmailCode(email);
    }

    //绑定邮箱
    @PostMapping(value = "/u/my/email/binding")
    ResultBean emailBinding(@RequestParam("email") String email,
                            @RequestParam("code") String code,
                            HttpServletRequest request) {
        long id = Long.parseLong((String) request.getAttribute("id"));
        log.info("浏览器发送的邮箱地址email是：" + email + ",验证码code是：" + code + ",用户id为：" + id);
        return personService.emailBinding(email, code, id);
    }

    //前台签到页
    @PostMapping(value = "/u/signing", produces = {"application/json;charset=UTF-8"})
    ResultBean signPage(HttpServletRequest request) {
        //从请求头中获取用户id
        Long id = Long.parseLong((String) request.getAttribute("id"));
        log.info("浏览器请求头中的用户id为：" + id);
        return personService.signPage(id);
    }

    //前台页签到
    @PutMapping(value = "/u/signing", produces = {"application/json;charset=UTF-8"})
    ResultBean sign(HttpServletRequest request) {
        //从请求头中获取用户id
        Long id = Long.parseLong((String) request.getAttribute("id"));
        log.info("浏览器请求头中的用户id为：" + id);
        return personService.sign(id);
    }

    //前台获取用户每日任务
    @GetMapping(value = "/u/signing/task", produces = {"application/json;charset=UTF-8"})
    ResultBean everydayTask(HttpServletRequest request) {
        Long id = Long.parseLong((String) request.getAttribute("id"));
        log.info("浏览器请求头中的用户id为：" + id);
        return personService.everydayTask(id);
    }

    //前台获取用户每日任务完成状态
    @GetMapping(value = "/u/signing/details", produces = {"application/json;charset=UTF-8"})
    ResultBean everydayTaskStatus(HttpServletRequest request) {
        Long id = Long.parseLong((String) request.getAttribute("id"));
        log.info("浏览器请求头中的用户id为：" + id);
        return personService.everydayTaskStatus(id);
    }

    //前台消息列表
    @GetMapping(value = "/u/message/ulist", produces = {"application/json;charset=Utf-8"})
    public ResultBean uSelectMessage(@RequestParam(value = "pageSize", required = false) Integer pageSize, @RequestParam(value = "pageNum", required = false) Integer pageNum, HttpServletRequest request) {
        log.info("controller层uSelectMessage方法传入的pageSize为" + pageSize + ",pageNum为" + pageNum);
        long userId = Long.parseLong((String) request.getAttribute("id"));
        return personService.uSelectMessage(pageSize, pageNum, userId);
    }

    //前台消息详情
    @GetMapping(value = "/u/umessage", produces = {"application/json;charset=Utf-8"})
    public ResultBean uSelectId(@RequestParam(value = "id") Long id, HttpServletRequest request) {
        log.info("controller层uSelectId方法传入的id为" + id);
        long userId = Long.parseLong((String) request.getAttribute("id"));
        return personService.uSelectId(id, userId);
    }

    //前台个人中心
    @PostMapping(value = "/u/my")
    public ResultBean myPerson(HttpServletRequest request) {
        Long id = Long.parseLong((String) request.getAttribute("id"));
        log.info("feign-web获取个人中心的用户id为：" + id);
        return personService.selectMyPersonById(id);
    }

    //一键阅读
    @GetMapping("/u/umessage/read")
    ResultBean selectRead(HttpServletRequest request) {
        Long userId = Long.parseLong((String) request.getAttribute("id"));
        return personService.selectRead(userId);
    }

    //个人资料
    @PostMapping(value = "/u/my/information")
    ResultBean selectMyInfoById(HttpServletRequest request) {
        Long id = Long.parseLong((String) request.getAttribute("id"));
        log.info("feign-web获取个人资料的用户id为:" + id);
        return personService.selectMyInfoById(id);
    }

    //修改个人资料
    @PutMapping(value = "/u/my/information", produces = {"application/json;charset=UTF-8"})
    ResultBean myPersonUpdate(@RequestBody User user, HttpServletRequest request) {
        Long id = Long.parseLong((String) request.getAttribute("id"));
        user.setId(id);
        log.info("feign-web传入的修改用户信息为：" + JsonUtil.toJsonString(user));
        return personService.myPersonUpdate(user);
    }

    @GetMapping(value = "/u/my/binding")
    ResultBean bindingUserInfo(HttpServletRequest request) {
        Long id = Long.parseLong((String) request.getAttribute("id"));
        log.info("feign-web获取到的绑定查询用户id为：" + id);
        return personService.bindingUserInfo(id);
    }

    //个人中心收藏列表
    @GetMapping(value = "/u/my/collection")
    PageBean myCollection(@RequestParam(value = "classify", required = false) Integer classify,
                          @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                          @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                          HttpServletRequest request) {
        Long userId = Long.parseLong((String) request.getAttribute("id"));
        log.info("feign-web传入查询用户收藏信息的id：" + userId + "，" + "classify：" + classify + "，" + "pageNum：" + pageNum + "，" + "pageSize：" + pageNum);
        return personService.myCollection(classify, pageNum, pageSize, userId);
    }

    //个人中心浏览记录
    @GetMapping(value = "/u/my/history")
    ResultBean myHistory(@RequestParam(value = "type", required = false) Integer type,
                         @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                         @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                         HttpServletRequest request) {
        Long userId = Long.parseLong((String) request.getAttribute("id"));
        log.info("feign-web传入查询用户收藏信息的id：" + userId + "，" + "classify：" + type + "，" + "pageNum：" + pageNum + "，" + "pageSize：" + pageNum);
        return personService.myHistory(type, pageNum, pageSize, userId);
    }

    //取消收藏
    @DeleteMapping(value = "/u/my/collection")
    ResultBean deleteCollection(@RequestParam(value = "courseId") long courseId,
                                HttpServletRequest request) {
        Long userId = Long.parseLong((String) request.getAttribute("id"));
        log.info("feign-web传入取消用户收藏信息id：" + userId + "," + "courseId" + courseId);
        return personService.deleteCollection(courseId, userId);
    }

    @DeleteMapping(value = "/u/my/collection/more")
    ResultBean deleteMoreCollection(HttpServletRequest request,
                                    @RequestParam(value = "courseId") String courseId,
                                    @RequestParam(value = "status") String status) {
        Long userId = Long.parseLong((String) request.getAttribute("id"));
        log.info("前台负载均衡传入的用户id：" + userId + ",传入的资料id：" + courseId + ".传入的状态" + status);
        return personService.deleteMoreCollection(userId, courseId, status);
    }

    //删除浏览记录
    @DeleteMapping(value = "/u/my/history")
    ResultBean deleteHistory(HttpServletRequest request) {
        Long userId = Long.parseLong((String) request.getAttribute("id"));
        log.info("feign-web传入取消用户收藏信息id：" + userId);
        return personService.deleteHistory(userId);
    }
}
